package com.yanqu.road.dao.impl.player;

import com.yanqu.road.dao.db.DbNameStrategy;
import com.yanqu.road.dao.impl.TempDataObjectDao;
import com.yanqu.road.dao.impl.logic.ResultSetParseEntityManger;
import com.yanqu.road.entity.player.UserPatronsSkill;

import java.sql.CallableStatement;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;

public class UserPatronsSkillDaoImpl extends TempDataObjectDao<UserPatronsSkill> {

	public List<UserPatronsSkill> getUserPatronsSkill(long userId,int patronsId) {
		String sql = "select * from t_u_patrons_skill where userId=? and patronsId = ?";
		List<UserPatronsSkill> dataMap = new ArrayList<>();
		ResultSet rs = null;
		CallableStatement call = null;
		try {
			getDb().setConnName(DbNameStrategy.getUserDb());
			call = getDb().getConnection().prepareCall(sql);
			call.setLong(1, userId);
			call.setLong(2, patronsId);
			rs = call.executeQuery();
			while (rs.next()) {
				UserPatronsSkill data = parse(rs);
				dataMap.add(data);
			}
		} catch (Exception e) {
			throw new RuntimeException(e);
		} finally {
			getDb().closeConnection(call, rs);
		}
		return dataMap;
	}

	public Map<Integer, List<UserPatronsSkill>> getUserPatronsSkillMap(long userId) {
		String sql = "select * from t_u_patrons_skill where userId=?";
		Map<Integer, List<UserPatronsSkill>> dataMap = new ConcurrentHashMap<>();
		ResultSet rs = null;
		CallableStatement call = null;
		try {
			getDb().setConnName(DbNameStrategy.getUserDb());
			call = getDb().getConnection().prepareCall(sql);
			call.setLong(1, userId);
			rs = call.executeQuery();
			while (rs.next()) {
				UserPatronsSkill data = parse(rs);
				if (!dataMap.containsKey(data.getPatronsId())) {
					dataMap.put(data.getPatronsId(), new ArrayList<>());
				}
				dataMap.get(data.getPatronsId()).add(data);
			}
		} catch (Exception e) {
			throw new RuntimeException(e);
		} finally {
			getDb().closeConnection(call, rs);
		}
		return dataMap;
	}

	@Override
	protected boolean addData(UserPatronsSkill data) {
		PreparedStatement call = null;
		String sql = "INSERT INTO t_u_patrons_skill(userId,patronsId,skillId,skillLv,type,skinSkillLevel,extraParams,isDelete) VALUES(?,?,?,?,?,?,?,?)";
		try {
			getDb().setConnName(DbNameStrategy.getUserDb());
			call = getDb().getConnection().prepareStatement(sql);
			int i = 1;
			call.setLong(i++, data.getUserId());
			call.setInt(i++, data.getPatronsId());
			call.setInt(i++, data.getSkillId());
			call.setInt(i++, data.getSkillLv());
			call.setInt(i++, data.getType());
			call.setInt(i++, data.getSkinSkillLevel());
			call.setString(i++, data.getExtraParams());
			call.setInt(i++, data.getIsDelete());
			return call.executeUpdate() > 0;
		} catch (Exception e) {
			logger.error(Thread.currentThread().getStackTrace()[1].getMethodName() + ":", e);
		} finally {
			getDb().closeConnection(call, null);
		}
		return false;
	}

	@Override
	protected boolean updateData(UserPatronsSkill data) {
		PreparedStatement call = null;
		String sql = "UPDATE t_u_patrons_skill SET skillLv=?,skinSkillLevel=?,extraParams=?,isDelete=? WHERE userId=? and patronsId=? and skillId=? and type=?";
		try {
			getDb().setConnName(DbNameStrategy.getUserDb());
			call = getDb().getConnection().prepareStatement(sql);
			int i = 1;
			call.setInt(i++, data.getSkillLv());
			call.setInt(i++, data.getSkinSkillLevel());
			call.setString(i++, data.getExtraParams());
			call.setInt(i++,data.getIsDelete());
			call.setLong(i++, data.getUserId());
			call.setInt(i++, data.getPatronsId());
			call.setInt(i++, data.getSkillId());
			call.setInt(i++, data.getType());
			return call.executeUpdate() > 0;
		} catch (Exception e) {
			logger.error(Thread.currentThread().getStackTrace()[1].getMethodName() + ":", e);
		} finally {
			getDb().closeConnection(call, null);
		}
		return false;
	}

	@Override
	protected UserPatronsSkill parseData(ResultSet rs) throws SQLException {
		return ResultSetParseEntityManger.initUserPatronsSkill(rs);
	}
}
